home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / dynamenu / dmdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  8.7 KB  |  290 lines

  1. // dmdoc.cpp
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1999 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12. //
  13. // Purpose: implementation of the CDynaMenuDoc class
  14. //
  15. // Functions:
  16. //      Most of this file was generated by AppWizard.  The functions
  17. //      which contain code specific to this sample are:
  18. //
  19. //      CDynaMenuDoc::CDynaMenuDoc()         -- document constructor
  20. //      CDynaMenuDoc::DoSelectColor()        -- handle text color commands
  21. //      CDynaMenuDoc::DoUpdateSelectColor()  -- update text color UI objects
  22. //      CDynaMenuDoc::GetMessageString()     -- get status msg for color cmds
  23. //      CDynaMenuDoc::OnCmdMsg()             -- perform command routing
  24. //      CDynaMenuDoc::OnColorOptions()       -- display color options dialog
  25.  
  26. #include "stdafx.h"
  27. #include "dynamenu.h"
  28. #include "dmdoc.h"
  29. #include "mainfrm.h"
  30. #include "coloropt.h"  // for CColorOptDlg
  31.  
  32. #ifdef _DEBUG
  33. #undef THIS_FILE
  34. static char BASED_CODE THIS_FILE[] = __FILE__;
  35. #endif
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // This table defines the possible text colors which will be displayed in
  39. // the color options dialog.  The colors selected in the options dialog
  40. // for a document will appear on the Color submenu when that document is
  41. // active.
  42. //
  43.  
  44. DynaMenuDef CDynaMenuDoc::m_aColorDef[NUM_TEXTCOLOR] =
  45. {
  46. #if _WIN32_WCE < 101 && !defined(_WIN32_WCE_EMULATION)
  47.     { IDS_BLACK,    ID_COLOR1, RGB(0, 0, 0) },
  48.     { IDS_DKGRAY,   ID_COLOR3, RGB(1, 1, 1) },
  49.     { IDS_LTGRAY,   ID_COLOR2, RGB(2, 2, 2) },
  50.     { IDS_WHITE,    ID_COLOR4, RGB(255, 255, 255) },
  51. #else
  52.     { IDS_BLACK,    ID_COLOR1, RGB(0, 0, 0) },
  53.     { IDS_DKGRAY,   ID_COLOR3, RGB(128, 128, 128) },
  54.     { IDS_LTGRAY,   ID_COLOR2, RGB(192, 192, 192) },
  55.     { IDS_WHITE,    ID_COLOR4, RGB(255, 255, 255) },
  56. #endif
  57. };
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CDynaMenuDoc
  61.  
  62. IMPLEMENT_DYNCREATE(CDynaMenuDoc, CDocument)
  63.  
  64. BEGIN_MESSAGE_MAP(CDynaMenuDoc, CDocument)
  65.     //{{AFX_MSG_MAP(CDynaMenuDoc)
  66.     ON_COMMAND(ID_COLOR_OPTIONS, OnColorOptions)
  67.     //}}AFX_MSG_MAP
  68. END_MESSAGE_MAP()
  69.  
  70. #define new DEBUG_NEW
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CDynaMenuDoc construction/destruction
  74.  
  75. CDynaMenuDoc::CDynaMenuDoc()
  76. {
  77.     // Initialize document data.  Start out by allowing all possible
  78.     // text colors and default to using the first one.
  79.     m_iColor = 0;
  80.     for (int i = 0; i < NUM_TEXTCOLOR; i++)
  81.        m_abAllowColor[i] = TRUE;
  82. }
  83.  
  84. CDynaMenuDoc::~CDynaMenuDoc()
  85. {
  86. }
  87.  
  88. BOOL CDynaMenuDoc::OnNewDocument()
  89. {
  90.     if (!CDocument::OnNewDocument())
  91.         return FALSE;
  92.  
  93.     return TRUE;
  94. }
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CDynaMenuDoc serialization
  98.  
  99. void CDynaMenuDoc::Serialize(CArchive& ar)
  100. {
  101.     if (ar.IsStoring())
  102.     {
  103.     }
  104.     else
  105.     {
  106.     }
  107. }
  108.  
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CDynaMenuDoc diagnostics
  111.  
  112. #ifdef _DEBUG
  113. void CDynaMenuDoc::AssertValid() const
  114. {
  115.     CDocument::AssertValid();
  116. }
  117.  
  118. void CDynaMenuDoc::Dump(CDumpContext& dc) const
  119. {
  120.     CDocument::Dump(dc);
  121. }
  122. #endif //_DEBUG
  123.  
  124. /////////////////////////////////////////////////////////////////////////////
  125. // CDynaMenuDoc implementation
  126.  
  127. //***********************************************************************
  128. // Function: CDynaMenuDoc::OnCmdMsg()
  129. //
  130. // Purpose:
  131. //    OnCmdMsg() is called by the framework to route and dispatch
  132. //    command messages and to handle the update of command
  133. //    user-interface objects.
  134. //
  135. //    Here we extend the standard command routing to intercept command
  136. //    messages with variable command IDs.  In this sample, the
  137. //    currently defined text color options are stored in
  138. //    CDynaMenuDoc::m_aColorDef.  Each element of this array contains
  139. //    the command ID associated with that color.  When a command ID
  140. //    is passed to this function, we search m_aColorDef for a matching
  141. //    command ID.  If one is found, we process the message.  Otherwise,
  142. //    we route the command to the standard OnCmdMsg processing.
  143. //
  144. // Parameters:
  145. //    nID           -- contains the command ID
  146. //    nCode         -- identifies the command notification code
  147. //    pExtra        -- used according to the value of nCode
  148. //    pHandlerInfo  -- if not NULL, filled in with pTarget and pmf
  149. //                     members of CMDHANDLERINFO structure, instead
  150. //                     of dispatching the command.  Typically NULL.
  151. //
  152. // Returns:
  153. //    nonzero if message handled, otherwise 0.
  154. //
  155. // Comments:
  156. //    See the documentation for CCmdTarget::OnCmdMsg() for further
  157. //    information.  Command routing is also discussed in tech note #21.
  158. //
  159. //***********************************************************************
  160. BOOL CDynaMenuDoc::OnCmdMsg(UINT nID, int nCode, void* pExtra,
  161.                             AFX_CMDHANDLERINFO* pHandlerInfo)
  162. {
  163.     // If pHandlerInfo is NULL, then handle the message
  164.     if (pHandlerInfo == NULL)
  165.     {
  166.         // Filter the commands sent to a text color menu option
  167.         for (int i = NUM_TEXTCOLOR-1; i >= 0; i--)
  168.         {
  169.             if (nID == m_aColorDef[i].m_nID)
  170.             {
  171.                 if (nCode == CN_COMMAND)
  172.                 {
  173.                     // Handle WM_COMMAND message
  174.                     DoSelectColor(i);
  175.                 }
  176.                 else if (nCode == CN_UPDATE_COMMAND_UI)
  177.                 {
  178.                     // Update UI element state
  179.                     DoUpdateSelectColor(i, (CCmdUI*)pExtra);
  180.                 }
  181.                 return TRUE;
  182.             }
  183.         }
  184.     }
  185.  
  186.     // If we didn't process the command, call the base class
  187.     // version of OnCmdMsg so the message-map can handle the message
  188.     return CDocument::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  189. }
  190.  
  191. //***********************************************************************
  192. // Function: CDynaMenuDoc::DoSelectColor()
  193. //
  194. // Purpose:
  195. //    DoSelectColor processes text color command messages for
  196. //    CDynaMenuDoc::OnCmdMsg(). It updates the document member variable
  197. //    m_iColor and calls UpdateAllViews() to redraw the document's
  198. //    views.
  199. //
  200. // Parameters:
  201. //    iColor -- index into CDynaMenuDoc::m_aColorDef for new
  202. //              color selection
  203. //
  204. // Returns:
  205. //    none.
  206. //
  207. // Comments:
  208. //    In the debug version, an assert is generated if the index is
  209. //    out of bounds or if the specified color is not currently
  210. //    enabled for the document.
  211. //
  212. //***********************************************************************
  213. void CDynaMenuDoc::DoSelectColor(int iColor)
  214. {
  215.     ASSERT(iColor >= 0 && iColor < NUM_TEXTCOLOR);
  216.     ASSERT(m_abAllowColor[iColor] == TRUE);
  217.  
  218.     m_iColor = iColor;
  219.     UpdateAllViews(NULL);
  220. }
  221.  
  222. //***********************************************************************
  223. // Function: CDynaMenuDoc::DoUpdateSelectColor()
  224. //
  225. // Purpose:
  226. //    DoUpdateSelectColor handled the updates to user-interface objects
  227. //    associated with text color command messages for
  228. //    CDynaMenuDoc::OnCmdMsg(). If the item is associated with the
  229. //    currently selected text color for the document, the item is checked.
  230. //
  231. // Parameters:
  232. //    iColor -- index into CDynaMenuDoc::m_aColorDef for new
  233. //              color selection
  234. //
  235. // Returns:
  236. //    none.
  237. //
  238. // Comments:
  239. //    In the debug version, an assert is generated if the index is
  240. //    out of bounds or if the item is disabled for the document.
  241. //
  242. //***********************************************************************
  243. void CDynaMenuDoc::DoUpdateSelectColor(int iColor, CCmdUI* pCmdUI)
  244. {
  245.     ASSERT(iColor >= 0 && iColor < NUM_TEXTCOLOR);
  246.     ASSERT(m_abAllowColor[iColor] == TRUE);
  247.  
  248.     pCmdUI->SetCheck(m_iColor == iColor);
  249.     pCmdUI->Enable(TRUE);
  250. }
  251.  
  252. /////////////////////////////////////////////////////////////////////////////
  253. // CDynaMenuDoc commands
  254.  
  255. //***********************************************************************
  256. // Function: CDynaMenuDoc::OnColorOptions()
  257. //
  258. // Purpose:
  259. //    OnColorOptions displays a dialog which allows the user to pick
  260. //    the text colors which should be displayed on the Color submenu
  261. //    when the document is active.
  262. //
  263. // Parameters:
  264. //    none.
  265. //
  266. // Returns:
  267. //    none.
  268. //
  269. // Comments:
  270. //    none.
  271. //
  272. //***********************************************************************
  273. void CDynaMenuDoc::OnColorOptions()
  274. {
  275.     CColorOptDlg dlg(this);
  276.     if (dlg.DoModal())
  277.     {
  278.         POSITION pos = GetFirstViewPosition();
  279.         CView* pview = GetNextView(pos);
  280.         ASSERT(pview != NULL);
  281.  
  282.         CMainFrame* pwnd = (CMainFrame*)pview->GetParentFrame();
  283.         ASSERT_KINDOF(CMainFrame, pwnd);
  284.  
  285.         // Force an update of the Color submenu
  286.         pwnd->RefreshColorMenu();
  287.     }
  288. }
  289.  
  290.